<?php

class ViewNews extends MY_Controller {

    public function __construct() {
        parent::__construct();
        
        if($this->session->userdata('user_type') != 1 || $this->session->userdata('user_type') == null){
            show_error("You have no permission here");
        }
    }

    function index() {
        $this->load->library('pagination');
        $this->load->library('table');

        
        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ViewNews/index'; 
        $config['total_rows'] = $this->db->get('NEWS')->num_rows(); 
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
        
        $this->pagination->initialize($config);

        $this->load->model('News_Model');
        $query = $this->News_Model->getAllNews($config['per_page'], $this->uri->segment(3));
        $result = $query->result();
        
        $this->table->set_heading('No', 'News Name', 'News Path', 'Manage'); //Create columns
        
        if($query->num_rows() > 0)
        {   
            for($i=0;$i<count($result);$i++):
                $row = $result[$i];
                $this->table->add_row(
                        $i+1, 
                        $row->NEWS_NAME, 
                        $row->NEWS_PATH,  
                        anchor( base_url().'index.php/ViewNews/getNews/'.$row->ID, '<img src="'.base_url().'images/view.png" alt="View" />' ).' '.anchor( base_url().'index.php/ViewNews/Delete/'.$row->ID, '<img src="'.base_url().'images/delete.png" alt="Delete" />', array('onClick' => "return confirm('Are you sure want to Delete?')") ) 
                        ); 
            endfor;

        }else{
            $this->table->add_row('No results found','','','');
        }

        $tmpl = array ( 
                        'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
                        'heading_cell_start'  => '<th class="table-header-repeat line-left"><a href>',
                        'heading_cell_end'    => '</a></th>',
                        'table_close'         => '</table>');

        $this->table->set_template($tmpl);

        $this->data['main_content'] = 'news';
        $this->load->view('includes/template', $this->data);
    }
    
    function getNews($newsID) {
        $this->load->Model('News_Model');
        $query = $this->News_Model->getNewsByID($newsID);
        
        if($query->num_rows() == 1) // if the user's credentials validated...
        {
            $row = $query->row();

            $news = array(
                                    'id' => $row->ID,
                                    'news_name' => $row->NEWS_NAME,
                                    'news_path' => $row->NEWS_PATH
            );
            $this->data['news'] = $news;
        }
        
        $newsName = array(
                'disabled'    => 'diabled',
                'id'    => 'newsName',
                'name'  => 'newsName'
            );
        
        $newsPath= array(
                'disabled'    => 'diabled',
                'id'    => 'newsSelect',
                'name'  => 'newsSelect'
            );
        
        
        $this->data['newsName'] = $newsName;
        $this->data['newsPath'] = $newsPath;

        $this->data['main_title'] = "View News";
        $this->data['action'] = 'view';
        $this->data['main_content'] = 'add_news';
        $this->load->view('includes/template', $this->data);
    }

    function Delete($newsID){

        $this->load->Model('News_Model');
        $this->News_Model->deleteNews($newsID);

        redirect('ViewNews');
    }
}
?>
